﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.VersionControl;
using UnityEngine;

public class ReplaceTreeMetarial : AssetPostprocessor
{
    private void OnPostprocessModel(GameObject model)
    {
               if (model == null)
                   return;
               if (assetPath.Contains("tree"))
               {
                   Renderer[] renderers = model.GetComponentsInChildren<Renderer>();
                   if (renderers == null)
                       return;
                   foreach (var renderer in renderers)
                   {
                       renderer.sharedMaterial = AssetDatabase.LoadAssetAtPath<Material>("Assets/Arts/Material/tree.mat");
                   }

                   var prefab = new GameObject();
                   prefab.gameObject.name = model.name;
                   var mf = prefab.AddComponent<MeshFilter>();
                   mf.sharedMesh = model.GetComponentInChildren<MeshFilter>().sharedMesh;
                   var mr = prefab.AddComponent<MeshRenderer>();
                   mr.sharedMaterial = AssetDatabase.LoadAssetAtPath<Material>("Assets/Arts/Material/tree.mat");
               }
    }

    private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets,
        string[] movedFromAssetPaths)
    {

    }
}
